<template>
	<view style="padding-top: 20px;">
		<view class="wrapper">
			<view class="infotop">
				<image :src="userinfo.avatar"></image>
			</view>
			<view class="infotext" >
				<text class="textname">{{userinfo.nickname}}</text>
				<text class="textphone">视信号：{{userinfo.account}}</text>
			</view>
		</view>
		<view class="scontent">
<!-- 			<view class="card" @click="gobeizhu">
				<text class="tit"  >备注</text>
			</view> -->
			<view class="fltwo"  @click="gopengyouquan">
				<view class="tit">云发布</view>
				<image src="/static/images/next.png"></image>
			</view>
			<view v-if="isOwner=='true'">
				<!-- <view class="card" @click="Prohibit">
					<text class="tit">禁言消息</text>
				</view> -->
				<view class="card" @click="black">
					<text class="tit">加入黑名单</text>
				</view>
				<view class="card" @click="Kicking">
					<text class="tit">踢出成员</text>
				</view>
				<view class="fltwo" @click="shagnxaifen" >
					<text class="tit">上下分</text>
					<image src="/static/images/next.png"></image>
				</view>
			</view>
			<view  class="fltwo"  style="border-bottom: 1px solid #eee;">
				<text class="tit">进群方式</text>
				<text class="yaoqing">{{fangshi}}</text>
				</text>
			</view>

			<view class="bottomtext" v-show="ismyfriend==true">
				<text @click="gomess">发送消息</text>

			</view>
			<view v-show="ismyfriend==false" @tap="addfriend" class="tjhaoyou">
				<text>添加好友</text>
			</view>

		</view>
	</view>
</template>

<script>
	var WebIM = require("../../utils/WebIM")["default"];
	export default {
		data() {
			return {
				token:'',
				isshow:false,
				uid:'',
				userinfo:{},
				ismyfriend:'',
				friend_name: "",
				qid:'',
				myname:'',
				q_id:'',
				guanli:'',
				hb_status:'',
				fangshi:''
			}
		},
		onLoad:function(data){
			console.log(data);
			this.myname=uni.getStorageSync('myUsername')
			this.uid=data.uid
			this.qid = data.qid
			this.q_id = data.q_id
			this.guanli= data.guanli
			this.isOwner = data.isOwner
			this.token=uni.getStorageSync('token')
			this.getuserinfo()
			WebIM.conn.getRoster().then( (res) => {
			  this.myList = res.data
			});
			uni.request({
				url:this.G_base + '/api/lz/isqinfo',
				method:'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'Authori-zation': 'Bearer ' + this.token
				},
				data:{
					id:this.q_id
				},
				success:res=>{
					this.hb_status = res.data.data.hb_status
				}
			})
		},
		methods: {
			//上下分
			shagnxaifen(){
				uni.navigateTo({
					url:'/pages/groupfriendinfo/sxfen?account='+this.userinfo.account+"&q_id="+this.q_id+"&roomid="+this.qid
				})
			},
			// 黑名单
			black(){
				uni.request({
					url:this.G_base + '/api/lz/qhmadd',
					method:'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authori-zation': 'Bearer ' + this.token
					},
					data:{
						qid:this.qid,
						account:this.uid
					},
					success:res=>{
						console.log(res)
						if(res.data.status==200){
							uni.showToast({
								title:res.data.msg
							})
							setTimeout(()=>{
								uni.navigateBack({
									delta:1
								})
							})
						}
						
					}
				})
			},
			// 禁言
			Prohibit(){
				uni.request({
					url:this.G_base + '/api/lz/qjy',
					method:'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authori-zation': 'Bearer ' + this.token
					},
					data:{
						qid:this.qid,
						account:this.uid
					},
					success:res=>{
						console.log(res)
						
							uni.showToast({
								title:"禁言成功"
							})
				
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						})
						
					}
				})
			},
			//Kicking(){}
			Kicking(){
				// 移除群组成员
				let option = {
				    groupId: this.qid,
				    username:this.userinfo.account                         // 群组成员名称
				};
				WebIM.conn.removeSingleGroupMember(option).then((res) => {
				    console.log("踢人",res)
				})
				uni.request({
					url:this.G_base + '/api/lz/qycuser',
					method:'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authori-zation': 'Bearer ' + this.token
					},
					data:{
						qid:this.qid,
						username:this.uid
					},
					success:res=>{
						if(res.data.status==200){
							uni.showToast({
								title:'踢出成功'
							})
							setTimeout(()=>{
								uni.navigateBack({
									delta:1
								},1000)
							})
						}
						
					}
				})
			},
			// 获取用户信息
			getuserinfo(){
				uni.request({
					url:this.G_base + '/api/lz/huinfo',
					method:'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authori-zation': 'Bearer ' + this.token
					},
					data:{
						account:this.uid,
						qid:this.qid
					},
					success:res=>{
						console.log(res)
						this.userinfo=res.data.data.data
						this.isfriend=res.data.data.data.is_hy
						this.friend_name=res.data.data.data.account
						this.fangshi = res.data.data.data.fangshi
						console.log(this.friend_name)
						this.myList .every(item=>{
						   if(item ==this.userinfo.account ) {
							   this.ismyfriend = true;
						       return false;
						   }else{
						       this.ismyfriend = false;
						       return true;
						   }
						});
					}
				})
			},
			
			// 添加好友
			addfriend(){
				uni.request({
					url:this.G_base + '/api/lz/zhy',
					method:'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authori-zation': 'Bearer ' + this.token
					},
					data:{
						account:this.friend_name
					},
					success:res=>{
						console.log(res)
						// console.log(res.status)
						if(res.data.status==200){
							uni.showToast({
								title:res.data.msg,
								icon:'none',
								success:()=>{
									this.getuserinfo()
								}
							})
							uni.redirectTo({
								url:"/pages/add_contact/add_contact"
							})
						}
						
						
						let me = this;
						let myName = uni.getStorageSync("myUsername");
						console.log(me.friend_name)
						if(me.friend_name == "" || me.friend_name.toLowerCase() == myName.toLowerCase()){
							this.$helper.toast('none', '添加失败');
							return;
						}
						this.$im.conn.subscribe({
							to: me.friend_name
						});
									
						// 判断当前是否存在该好友
						let rosters = {
							success: function(roster){
								console.log('success')
								var member = [];
								for(let i = 0; i < roster.length; i++){
									if(roster[i].subscription == "both"){
										member.push(roster[i]);
									}
								}
								if(me.isExistFriend(me.friend_name, member)){
									me.$helper.toast('none', '已经是你的好友');
								}
								else{
									me.$helper.toast('none', '已经发出好友申请');
								}
								me.isdisable = true;
								// console.log(member)
							}
						};
						this.$im.conn.getRoster(rosters);
					}
				})
			},
			isExistFriend(name, list) {
				for (let index = 0; index < list.length; index++) {
					if (name == list[index].name) {
						console.log("已经是你的好友了")
						return true
					}
				}
				
				console.log("不是你的好友请添加")
				return false
			},
			gobeizhu(){
				uni.navigateTo({
					url:"../beizhu/beizhu?nickname="+this.userinfo.nickname
				})
			},
			gofenxiang(){
				uni.navigateTo({
					url:"../mine/mycode/mycode"
				})
			},
			gomess(){
				// uni.navigateTo({
				// 	url:"/pages/chart/chart"
				// })
				let name = uni.getStorageSync('myUsername')
				var nameList = {
					myName: name,
					your: this.friend_name
				};
				uni.navigateTo({
					url: "../chatroom/chatroom?username=" + JSON.stringify(nameList)
				});
			},
			test(){
				uni.navigateBack({
					delta:1
				})
			},
			goback() {
				console.log("Fanhu")
				uni.navigateBack({
					delta:1
				})
			},
			upfen(){
				uni.navigateTo({
					url:"../friendt/friendt"
				})
			},
			gopengyouquan(){
				uni.navigateTo({
					url:"../notification/friend_notification?name="+this.uid
				})
			}
		},
	}
</script>
<style>
	.bottomtext{
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-top: 50upx;
	}
	.bottomtext text{
		width: 100%;
		height: 80upx;
		line-height: 80upx;
		text-align: center;
		color: rgba(16, 16, 16, 1);
		font-size: 16px;
	}
	.tjhaoyou{
		width: 100%;
		height: 80upx;
		text-align: center;
		line-height: 80upx;
		margin-top: 50upx;
		font-size: 16px;
	}
	.tit{
		color: rgba(16, 16, 16, 1);
		font-size: 14px;
		text-align: left;
		font-family: Microsoft YaHei;
	}
	.fenshu{
		color: rgba(16, 16, 16, 1);
		font-size: 14px;
		text-align: left;
		font-family: Microsoft YaHei;
		margin-left: 100px;
	}
	.yaoqing{
		color: rgba(16, 16, 16, 1);
		font-size: 14px;
		font-family:Microsoft YaHei;
		margin-left: 180px;
	}
	.scontent{
		width: 100%;
		padding:0 35upx ;
		box-sizing: border-box;
		margin-top: 20upx;
	}
	.card{
		height: 50px;
		border-bottom: 1px solid #eee;
		line-height: 40px;
	}
	.fltwo {
		height: 50px;
		border-bottom: 1px solid #eee;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.fltwo image{
		width: 55upx;
		height: 55upx;
	}

	.wrapper .infotext {
		display: flex;
		flex-direction: column;
		line-height: 55upx;
		margin-left: 20upx;
	}
	.textphone{
		font-size: 15px;
		color: #999;
	}

	.wrapper {
		width: 100%;
		padding:0 35upx ;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.infotop image {
		width: 60px;
		height: 60px;
		border-radius: 80px;
	}
</style>
